import tkinter as tk
from tkinter import filedialog
import argparse
import io
import sys

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf8")

parser = argparse.ArgumentParser(description='ArgUtils')
parser.add_argument('-f', type=str, default='folder', help="select type")
args = parser.parse_args()
# 传递参数 -f 为folder 选择文件夹 为file选择文件


root = tk.Tk()  # 创建根窗口
root.wm_attributes('-topmost', 1)
root.withdraw()  # 隐藏根窗口

def chooceFolder():
    folder_path = filedialog.askdirectory()
    if folder_path == '':
        print('{"code": "400"}')
        return
    print('{"code": "200", "data": "%s"}' % folder_path)
def chooceFile():
    file_path = filedialog.askopenfilename()
    if file_path == '':
        print('{"code": "400"}')
        return
    print('{"code": "200", "data": "%s"}' % file_path)

def entry():
    if (args.f == "folder"):
        chooceFolder()
    if (args.f == "file"):
        chooceFile()

if __name__ == "__main__":
    entry()